package com.rules.admin.controller;

import com.rules.admin.utils.ResultUtils;
import com.rules.admin.entity.UnitCaseExpectAction;
import com.rules.admin.entity.UnitCaseExpectAsset;
import com.rules.admin.entity.UnitCaseExpectResult;

import com.rules.admin.service.UnitCaseExpectActionService;
import com.rules.admin.service.UnitCaseExpectAssetService;
import com.rules.admin.service.UnitCaseExpectResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/unitcases/"})
public class UnitCaseExpectController {
   @Autowired
   private UnitCaseExpectAssetService unitCaseExpectAssetService;
   @Autowired
   private UnitCaseExpectActionService unitCaseExpectActionService;
   @Autowired
   private UnitCaseExpectResultService unitCaseExpectResultService;
   private static String mes = "删除失败";

   @RequestMapping(
      value = {"/assets"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createAsset(@RequestBody UnitCaseExpectAsset expectAsset) {
      this.unitCaseExpectAssetService.save(expectAsset);
      return ResultUtils.getSuccessResultData(expectAsset);
   }

   @RequestMapping(
      value = {"/assets/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteAsset(@PathVariable("id") Long id) {
      boolean result = this.unitCaseExpectAssetService.removeById(id);
      return result ? ResultUtils.getSuccessResultData() : ResultUtils.getFaildResultData(new String[]{mes});
   }

   @RequestMapping(
      value = {"/assets/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateAsset(@RequestBody UnitCaseExpectAsset expectAsset) {
      this.unitCaseExpectAssetService.save(expectAsset);
      return ResultUtils.getSuccessResultData(expectAsset);
   }

   @RequestMapping(
      value = {"/assets/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCaseExpectAsset> findAsset(@PathVariable("id") Long unitCaseId) {
      return this.unitCaseExpectAssetService.getAllAssetByCaseId(unitCaseId);
   }

   @RequestMapping(
      value = {"/assets"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCaseExpectAsset> newfindAsset(Long unitCaseId) {
      return this.unitCaseExpectAssetService.getAllAssetByCaseId(unitCaseId);
   }

   @RequestMapping(
      value = {"/results"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createResult(@RequestBody UnitCaseExpectResult expectResult) {
      List<UnitCaseExpectResult> list = this.unitCaseExpectResultService.findExpectResultByCaseId(expectResult.getUnitCaseId());
      if (!list.isEmpty()) {
         return ResultUtils.getFaildResultData(new String[]{"结果集期望已存在，新增失败！"});
      } else {
         this.unitCaseExpectResultService.save(expectResult);
         return ResultUtils.getSuccessResultData(expectResult);
      }
   }

   @RequestMapping(
      value = {"/results/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteResult(@PathVariable("id") Long id) {
      boolean result = this.unitCaseExpectResultService.removeById(id);
      return result ? ResultUtils.getSuccessResultData() : ResultUtils.getFaildResultData(new String[]{mes});
   }

   @RequestMapping(
      value = {"/results/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateResult(@RequestBody UnitCaseExpectResult expectResult) {
      this.unitCaseExpectResultService.save(expectResult);
      return ResultUtils.getSuccessResultData(expectResult);
   }

   @RequestMapping(
      value = {"/results/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public UnitCaseExpectResult findResult(@PathVariable("id") Long unitCaseId) {
      return this.unitCaseExpectResultService.findOneExpectResultByCaseId(unitCaseId);
   }

   @RequestMapping(
      value = {"/results"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCaseExpectResult> newfindResult(Long unitCaseId) {
      return this.unitCaseExpectResultService.findExpectResultByCaseId(unitCaseId);
   }

   @RequestMapping(
      value = {"/actions"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createAction( @RequestBody UnitCaseExpectAction expectAction) {
      this.unitCaseExpectActionService.save(expectAction);
      return ResultUtils.getSuccessResultData(expectAction);
   }

   @RequestMapping(
      value = {"/actions/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteAction(@PathVariable("id") Long id) {
      boolean result = this.unitCaseExpectActionService.removeById(id);
      return result ? ResultUtils.getSuccessResultData() : ResultUtils.getFaildResultData(new String[]{mes});
   }

   @RequestMapping(
      value = {"/actions/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateAction(@RequestBody UnitCaseExpectAction expectAction) {
      this.unitCaseExpectActionService.save(expectAction);
      return ResultUtils.getSuccessResultData(expectAction);
   }

   @RequestMapping(
      value = {"/actions"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<UnitCaseExpectAction> findAction(@PathVariable("unitCaseId") Long unitCaseId) {
      return this.unitCaseExpectActionService.getAllActionByCaseId(unitCaseId);
   }
}
